<template>
  <div class="login-container">
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="square square-1"></div>
      <div class="square square-2"></div>
    </div>

    <div class="login-card">
      <div class="brand-section">
        <div class="logo">
          <svg viewBox="0 0 100 100" class="logo-icon">
            <circle cx="50" cy="50" r="40" fill="#333" />
            <circle cx="50" cy="50" r="25" fill="#fff" />
            <circle cx="50" cy="50" r="15" fill="#333" />
          </svg>
        </div>
        <h1 class="brand-title">{{ title }}</h1>
        <p class="brand-subtitle">Professional & Elegant</p>
      </div>

      <div class="login-form-container">
        <div class="welcome-text">
          <h2>欢迎回来</h2>
          <p>请登录您的账号</p>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          label-width="0"
          class="login-form"
          :rules="loginRules"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Icon name="User" /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              placeholder="密码"
              size="large"
              class="custom-input"
              show-password
            >
              <template #prefix>
                <el-icon class="input-icon"><Icon name="Lock" /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-button
            type="primary"
            size="large"
            class="login-button"
            @click="onSubmit(loginFormRef)"
            :loading="loading"
          >
            {{ loading ? '登录中...' : '立即登录' }}
          </el-button>
        </el-form>

        <div style="width: 100%; color: #8a8a8a">
          <text style="float: left">admin/123456</text>
          <text style="float: right">editor/123456</text>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Icon from '@/components/Icon/index.vue'
import useUserStores from '@/stores/modules/user'

const router = useRouter()
const route = useRoute()

const userStore = useUserStores()

const loginForm = reactive({
  username: 'admin',
  password: '123456',
})

const loginFormRef = ref<FormInstance>()

const loading = ref(false)

const loginRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' },
  ],
})

const redirect = ref<string | undefined>(undefined)
watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && (newRoute.query.redirect as string | undefined)
  },
  { immediate: true },
)

const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.userLogin(loginForm.username, loginForm.password)
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce(
          (acc: Record<string, any>, cur) => {
            if (cur !== 'redirect') {
              acc[cur] = query[cur]
            }
            return acc
          },
          {} as Record<string, any>,
        )
        router.push({ path: redirect.value || '/', query: otherQueryParams })
      } finally {
        loading.value = false
      }
    }
  })
}

const title = computed(() => {
  return import.meta.env.VITE_APP_TITLE
})
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7f1 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  overflow: hidden;
  position: relative;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;

  .circle,
  .square {
    position: absolute;
    background: rgba(0, 0, 0, 0.03);
    border-radius: 50%;
    animation: float 15s infinite linear;
  }

  .circle-1 {
    width: 150px;
    height: 150px;
    top: 10%;
    left: 5%;
    animation-duration: 20s;
  }

  .circle-2 {
    width: 80px;
    height: 80px;
    top: 70%;
    left: 15%;
    animation-duration: 25s;
    animation-direction: reverse;
  }

  .circle-3 {
    width: 120px;
    height: 120px;
    top: 30%;
    right: 10%;
    animation-duration: 30s;
  }

  .square {
    border-radius: 0;
    transform: rotate(45deg);
  }

  .square-1 {
    width: 100px;
    height: 100px;
    bottom: 20%;
    right: 20%;
    animation-duration: 18s;
  }

  .square-2 {
    width: 60px;
    height: 60px;
    top: 40%;
    left: 25%;
    animation-duration: 22s;
    animation-direction: reverse;
  }
}

.login-card {
  display: flex;
  width: 900px;
  height: 550px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  overflow: hidden;
  z-index: 1;
  position: relative;

  @media (max-width: 992px) {
    width: 95%;
    max-width: 600px;
    flex-direction: column;
    height: auto;
  }

  .brand-section {
    flex: 1;
    background: linear-gradient(135deg, #333333 0%, #555555 100%);
    color: white;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;

    @media (max-width: 992px) {
      padding: 30px 20px;
    }

    .logo {
      width: 100px;
      height: 100px;
      margin-bottom: 30px;

      .logo-icon {
        width: 100%;
        height: 100%;
      }
    }

    .brand-title {
      font-size: 32px;
      font-weight: 300;
      margin-bottom: 10px;
      letter-spacing: 1px;
    }

    .brand-subtitle {
      font-size: 16px;
      opacity: 0.8;
      margin: 0;
    }
  }

  .login-form-container {
    flex: 1;
    padding: 50px 40px;
    display: flex;
    flex-direction: column;

    @media (max-width: 992px) {
      padding: 30px 20px;
    }

    .welcome-text {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        font-size: 28px;
        color: #333;
        margin-bottom: 10px;
        font-weight: 400;
      }

      p {
        color: #666;
        font-size: 14px;
        margin: 0;
      }
    }

    .login-form {
      flex: 1;

      .custom-input {
        margin-bottom: 25px;

        :deep(.el-input__wrapper) {
          border-radius: 8px;
          padding: 15px 20px;
          box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
          transition: all 0.3s ease;

          &:hover {
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
          }

          &.is-focus {
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
          }
        }

        .input-icon {
          font-size: 18px;
          color: #666;
        }
      }

      :deep(.el-form-item) {
        margin-bottom: 25px;
      }

      .login-button {
        width: 100%;
        padding: 16px;
        border-radius: 8px;
        font-size: 16px;
        font-weight: 500;
        letter-spacing: 1px;
        transition: all 0.3s ease;
        box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
        height: 50px;
        margin-top: 10px;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
        }
      }
    }
  }
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(10px, 15px) rotate(5deg);
  }
  50% {
    transform: translate(20px, 5px) rotate(0deg);
  }
  75% {
    transform: translate(10px, 15px) rotate(-5deg);
  }
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
}
</style>
